package com.itboye.textileexpres.myset;

import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.textileexpes.beans.ReceiveAddressBean;
import com.itboye.textileexpes.presenter.ReceiveAddressPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.SPUtils;
/**
 * 修改收货地址
 */
public class ActivityAddressUpdate extends BaseActivity implements Observer {

	String country = "", province, city, district, provinceId, cityId,
			districtId;

	protected String cityTxt;
	private TextView edregion;
	private Button btn_send;
	private EditText edName;
	private EditText edPhoneName;
	private EditText edaddressName;
	private EditText edPostcodeName;
	ReceiveAddressBean model = new ReceiveAddressBean();;
	ImageView back;
	TextView title;
	ImageView img_other;
	TextView txt_save;
	String phone;
	String name;
	String address;
	String detailAddress;
	String postCode;
	ToggleButton default_address;
	ReceiveAddressPresenter addressPresenter;
	String addressid;
	private String isDefault;
	private String PROVINCE;
	private String PROVINCEID;
	private String CITY;
	private String CITYID;
	private String AREA;
	private String AREAID;
	private String COUNTRY;
	RelativeLayout delete_rel;
	private String addCity;
	private String countryID;
	private String provinceID;
	private String cityID;
	private String areaID;

	private String countryNmae;
	@SuppressWarnings("unused")
	private String provinceNmae;
	private String cityNmae;
	private String areaNmae;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_address_update;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText(getResources().getString(R.string.modifyAddress));
		img_other.setVisibility(View.GONE);
		txt_save.setVisibility(View.VISIBLE);
		countryID = getIntent().getStringExtra("countryid");
		name = getIntent().getStringExtra("name");
		phone = getIntent().getStringExtra("phone");
		address = getIntent().getStringExtra("address");
		detailAddress = getIntent().getStringExtra("detailinfo");
		postCode = getIntent().getStringExtra("postcode");
		addressid = getIntent().getStringExtra("id");
		isDefault = getIntent().getStringExtra("isdefault");
		PROVINCE = getIntent().getStringExtra("provice");
		PROVINCEID = getIntent().getStringExtra("proviceid");
		CITY = getIntent().getStringExtra("city");
		CITYID = getIntent().getStringExtra("cityid");
		AREA = getIntent().getStringExtra("area");
		AREAID = getIntent().getStringExtra("areaid");
		COUNTRY = getIntent().getStringExtra("country");
		edName.setText(name);
		edPhoneName.setText(phone);
		System.out.println("tiantian" + countryID + "\n" + CITYID + "\n"
				+ AREAID);

		IntentFilter intentFilterAvart2 = new IntentFilter("province");
		this.registerReceiver(avartBroadcastReceiver, intentFilterAvart2);
		// 其他国家
		IntentFilter intentFilterAvart3 = new IntentFilter("AddressBeans");
		this.registerReceiver(avartBroadcastReceiverTitle, intentFilterAvart3);

		edregion.setText(address);
		edPostcodeName.setText(postCode);
		edaddressName.setText(detailAddress);
		addressPresenter = new ReceiveAddressPresenter(this);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		txt_save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onEnaBled();
			}
		});
		edregion.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent();
				intent.setClass(ActivityAddressUpdate.this,
						ActivityDiZhiType.class);
				startActivity(intent);

			}

		});
		if ("1".equals(isDefault) || isDefault == "1") {
			delete_rel.setVisibility(View.GONE);
		} else {
			delete_rel.setVisibility(View.VISIBLE);
		}

		default_address
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							addressPresenter.Default(IsMobileNumPhone.isUid(),
									addressid);
						}
					}
				});

	}

	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {

			addCity = (String) SPUtils.get(ActivityAddressUpdate.this, null,
					SpPublicParams.TilteCity, "");

			edregion.setText(addCity);
		};
	};
	BroadcastReceiver avartBroadcastReceiverTitle = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			addCity = (String) SPUtils.get(ActivityAddressUpdate.this, null,
					SpPublicParams.AddressBeans, "");
			addCity = (String) SPUtils.get(ActivityAddressUpdate.this, null,
					SpPublicParams.AddressBeans, "");

			edregion.setText(addCity);

			countryID = (String) SPUtils.get(ActivityAddressUpdate.this, null,
					SpPublicParams.COUTRYID, "");
			provinceID = (String) SPUtils.get(ActivityAddressUpdate.this, null,
					SpPublicParams.PROVINCEID, "");

			cityID = (String) SPUtils.get(ActivityAddressUpdate.this, null,
					SpPublicParams.PROVINCECITYID, "");
			areaID = (String) SPUtils.get(ActivityAddressUpdate.this, null,
					SpPublicParams.AREAID, "");

			countryNmae = (String) SPUtils.get(ActivityAddressUpdate.this,
					null, SpPublicParams.COUNTRY, "");
			provinceNmae = (String) SPUtils.get(ActivityAddressUpdate.this,
					null, SpPublicParams.PROVINCE, "");

			cityNmae = (String) SPUtils.get(ActivityAddressUpdate.this, null,
					SpPublicParams.PROVINCECITY, "");
			areaNmae = (String) SPUtils.get(ActivityAddressUpdate.this, null,
					SpPublicParams.AREA, "");
			edregion.setText(addCity);
		};
	};

	public void onEnaBled() {
		String people = edName.getText().toString().trim();
		String phone = edPhoneName.getText().toString().trim();
		String addrDetail = edaddressName.getText().toString();
		String code = edPostcodeName.getText().toString().trim();
		if (default_address.isChecked()) {
			isDefault = "0";
		} else {
			isDefault = "1";
		}
		if ("".equals(countryNmae) || countryNmae == null || countryNmae == "") {
			model.setProvinceid(PROVINCEID);
			model.setCityid(CITYID);
			model.setcountryId(countryID);
			model.setAreaid(AREAID);
			model.setArea(AREA);
			model.setProvince(province);
			model.setCityid(cityID);
			model.setCity(CITY);
			model.setArea(AREA);
			model.setCity(CITY);
			model.setCountry(COUNTRY);

		} else {
			System.out.println("doudoude" + countryNmae + provinceNmae
					+ cityNmae);
			model.setId(addressid);
			model.setUid(IsMobileNumPhone.isUid());
			model.setCountry(countryNmae);
			model.setProvinceid(provinceID);
			model.setCityid(cityID);
			model.setAreaid(areaID);
			model.setDetailinfo(addrDetail);
			model.setContactname(people);
			model.setMobile(phone);
			model.setpostalCode(code);
			model.setArea(areaNmae);
			model.setProvince(provinceNmae);
			model.setCity(cityNmae);
			model.setcountryId(countryID);
			model.setisDefault(isDefault);
			addressPresenter.update(model);
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == ReceiveAddressPresenter.DefaultSuccessEvent) {
				// toast(entity.getData() + "");
				// finish();
				if (default_address.isChecked()) {
					isDefault = "0";
				} else {
					isDefault = "1";
				}
			} else if (entity.getEventType() == ReceiveAddressPresenter.DefaultFailureEvent) {
				toast(entity.getData() + "");
			}
			if (entity.getEventType() == ReceiveAddressPresenter.UpdateSuccessEvent) {
				toast(entity.getData() + "");
				finish();
			} else {
				toast(entity.getData() + "");
			}
		}
	}

}
